package com.bss.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.andy.basics.MessageHandler;
import com.andy.basics.Service;
import com.bss.dao.CountryDao;
import com.bss.dao.PlaceDao;
import com.bss.dao.StateDao;
import com.bss.model.Building;
import com.bss.model.Country;
import com.bss.model.Place;
import com.bss.model.State;
import com.bss.service.PlaceService;

@Controller
@Scope("request")
public class PlaceController extends com.andy.basics.Controller<Place> {

	@Autowired
	private PlaceService placeService;

	@Autowired
	private CountryDao countryDao;

	private Date date = new Date();

	@Autowired
	private StateDao stateDao;

	@Autowired
	private PlaceDao placeDao;

	@Override
	public Date getDate() {
		return this.date;
	}

	@Override
	public void setDate(Date date) {
		this.date = date;
	}

	@Override
	public Service<Place> getService() {
		return this.placeService;
	}

	@Override
	public Place create() {
		return null;
	}

	@Override
	public String getModelName() {
		return "Place";
	}

	protected boolean validate(final Building building, final MessageHandler handler) {
		// List<Building> b = this.buildingService.find("name='" +
		// building.getName() + "'");
		// PlaceController.logger.debug("Number of entries with building name "
		// + building.getName() + ": " + b.size());
		// if (b.size() > 0) {
		// handler.addMessage("Name", "Building with name: " +
		// building.getName() + " already exists ! ");
		// return false;
		// }
		return true;
	}

	public Place getPlace() {
		return this.placeService.getPlace();
	}

	public State getState() {
		return this.placeService.getState();
	}

	public Country getCountry() {
		return this.placeService.getCountry();
	}

	public void addPlace() {
		this.add("place");
	}

	public void addState() {
		this.add("state");
	}

	public void addCountry() {
		this.add("country");
	}

	public void add(String type) {
		this.setError(false);
		try {
			if ("place".equals(type)) {
				if (this.getPlace().getName().isEmpty()) {
					throw new Exception("Place name is empty !");
				}
				this.getPlace().setDate(this.getDate());
				this.placeService.addPlace();
			} else if ("state".equals(type)) {
				if (this.getState().getName().isEmpty()) {
					throw new Exception("State name is empty !");
				}
				this.getState().setDate(this.getDate());
				this.placeService.addState();
			} else if ("country".equals(type)) {
				if (this.getCountry().getName().isEmpty()) {
					throw new Exception("Country name is empty !");
				}
				this.getCountry().setDate(this.getDate());
				this.placeService.addCountry();
			}
		} catch (Exception e) {
			this.setError(true);
			this.setErrors(new ArrayList<String>());
			this.getErrors().add(
					"Error while adding: Either " + type + " is already added or " + type + " name is invalid. Error: "
							+ e.getMessage());
		}

	}

	public Place getSelectedPlace() {
		return this.placeService.getSelectedPlace();
	}

	public void setSelectedPlace(Place selectedPlace) {
		this.placeService.setSelectedPlace(selectedPlace);
	}

	public Set<State> getSelectedState() {

		State state = this.placeService.getSelectedState();
		if (state == null) {
			return null;
		}
		Set<State> c = new HashSet<State>();
		c.add(state);
		return c;
	}

	public void setSelectedState(Set<State> states) {
		Iterator<State> itr = states.iterator();
		if (itr.hasNext()) {
			this.placeService.setSelectedState(itr.next());
		}
	}

	public List<Country> getCountries() {
		return this.countryDao.getAll();
	}

	public void setCountries(List<Country> countries) {

	}

	public List<State> getStates() {
		Country country = this.placeService.getSelectedCountry();

		if (country == null) {
			return null;
		}

		return this.stateDao.find("country.name = '" + country.getName() + "'");
	}

	public void setStates(List<State> states) {

	}

	public List<Place> getPlaces() {

		State state = this.placeService.getSelectedState();

		if (state == null) {
			return null;
		}
		return this.placeDao.find("state.name='" + state.getName() + "'");
	}

	public void setPlaces(List<Place> places) {

	}

	public void setSelectedCountry(Set<Country> countries) {
		Iterator<Country> itr = countries.iterator();
		if (itr.hasNext()) {
			this.placeService.setSelectedCountry(itr.next());
		}
	}

	public Set<Country> getSelectedCountry() {

		Country country = this.placeService.getSelectedCountry();

		if (country == null) {
			return null;
		}
		Set<Country> c = new HashSet<Country>();
		c.add(this.placeService.getSelectedCountry());
		return c;
	}

	public void selectCountry() {
		this.placeService.setSelectedState(null);
		this.placeService.setSelectedPlace(null);
	}

	public void selectState() {
	}
}
